<?php
	
	include('../classes/clsDB.php');
	include('../classes/clsStats.php');
	include('../config.inc.php');
	$r=new ReturnRecord();
	$SQL="SELECT domain FROM affiliateDomains WHERE id=$_GET[d]";
	//print $SQL;
	$sq2=$r->rawQuery($SQL);  
	$myrow = mysql_fetch_row($sq2);
	if(eregi($myrow[0],$_GET['dname'])){
		if(!$_SESSION['sid']){
			$stats=new Count_visitors("Impression",$_GET['aid'],$_GET['ab']);
			$stats->delay = 1; // how often (in hours) a visitor is registered in the database 
			$sid=$stats->insert_new_visit($_GET['d']);
			if($sid!=-1) $_SESSION['sid']=$sid;
		}else{
			$sid=$_SESSION['sid'];
		}
		$CookieDurationDays=365;
		setcookie("AffCode",$_GET['aid'],time()+60*60*24*$CookieDurationDays,'/',$host);
		setcookie("AffDomainID",$_GET['d'],time()+60*60*24*$CookieDurationDays,'/',$host);
		setcookie("AffSid",$sid,time()+60*60*24*$CookieDurationDays,'/',$host);
		$SQL="SELECT TargetURL,BType,HTMLCode,Image,affiliateBanners.id FROM affiliateBanners,affiliateJoinedPrograms WHERE affiliateBanners.affiliateProgramsID=affiliateJoinedPrograms.affiliateProgramsID AND  affiliateBanners.id=$_GET[ab] AND affiliateJoinedPrograms.affiliateID=$_GET[aid]";
		//print $SQL;
		$sq2=$r->rawQuery($SQL);  
		$myrow = mysql_fetch_row($sq2);
	}
		$TargetURL="http://$host".$rootdir."codes/redir.php?id=$myrow[4]&aid=$_GET[aid]&ab=$_GET[ab]&d=$_GET[d]&sid=$sid";
	
	
?>var mytext = '<?
		
		if($myrow[1]=="Image"){
		?><a href="<?=$TargetURL;?>"><img src="http://<?=$host.$rootdir;?>banners/<?=$myrow[3]?>" border="0"></a><?
		}else{
			?><a href="<?=$TargetURL;?>"><?=$myrow[2]?></a><?
		};
		
		?>';
document.write(mytext);
